﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;
using MSBTC.ReportViewer;

namespace MSBTC.WinForms
{
    public partial class ClientForm : Form
    {
        private string Datamanip;
        private int rowselect;
        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        Customer dbcus = new Customer();

        public ClientForm()
        {
            InitializeComponent();
        }

        private void ClientForm_Load(object sender, EventArgs e)
        {
            RefreshFields();
            RefreshDB();
        }

        #region functions
        private void RefreshDB()
        {
            var cus = (from c in dbcontext.Customers
                       select c).ToList();

            LVClientInfo.Items.Clear();
            if (cus != null)
            {
                
                foreach (var sqlCus in cus)
                {
                    ListViewItem lvitems = LVClientInfo.Items.Add(sqlCus.Customerid_PK.ToString());
                    lvitems.SubItems.Add(sqlCus.Customer_Name.ToString());
                    lvitems.SubItems.Add(sqlCus.Address.ToString());
                    lvitems.SubItems.Add(sqlCus.City.ToString());
                    lvitems.SubItems.Add(sqlCus.Description.ToString());
                    lvitems.SubItems.Add(sqlCus.Email_Address.ToString());
                    lvitems.SubItems.Add(sqlCus.Contacts.ToString());
                    
                }
            }
            this.Refresh();
        }

        private void ClearFields()
        {
            txtID.Clear();
            txtCname.Clear();
            txtAddr.Clear();
            txtCity.Clear();
            txtDesc.Clear();
            txtEadd.Clear();
            txtCnum.Clear();
            txtSearch.Clear();

        }

        private void DisableFields()
        {
            txtCname.Enabled = false;
            txtAddr.Enabled = false;
            txtCity.Enabled = false;
            txtDesc.Enabled = false;
            txtEadd.Enabled = false;
            txtCnum.Enabled = false;

        }

        private void EnableFields()
        {
            txtCname.Enabled = true;
            txtAddr.Enabled = true;
            txtCity.Enabled = true;
            txtDesc.Enabled = true;
            txtEadd.Enabled = true;
            txtCnum.Enabled = true;

        }

        private void RefreshFields()
        {
            DisableFields();
            ClearFields();
            RefreshDB();

            btnCNew.Enabled = true;
            btnCDel.Enabled = false;
            btnCEdit.Enabled = false;
            btnClear.Enabled = true;
            btnCSave.Enabled = false;
        }
        #endregion
        
        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearFields();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshFields();
            LVClientInfo.Enabled = true;
        }

        private void btnCNew_Click(object sender, EventArgs e)
        {
            Datamanip = "Add";

            EnableFields();
            ClearFields();

            LVClientInfo.Enabled = false;
            btnCSave.Enabled = true;
            btnCNew.Enabled = false;
        }

        private void btnCEdit_Click(object sender, EventArgs e)
        {
            Datamanip = "Edit";

            EnableFields();

            btnCSave.Enabled = true;
            btnCNew.Enabled = false;
            btnCDel.Enabled = false;
            btnCEdit.Enabled = false;
            
        }

        private void btnCSave_Click(object sender, EventArgs e)
        {
            if (txtCname.Text == null || txtAddr.Text == null || txtCity.Text == null || txtCnum.Text == null)
            {
                MessageBox.Show("Please fill up the empty fields", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {

                if (Datamanip == "Add")
                {
                    var add = (from c in dbcontext.Customers
                               where c.Customer_Name == txtCname.Text
                               select c).ToList();

                    if (add.Count == 0)
                    {
                       
                        dbcus.Customer_Name = txtCname.Text;
                        dbcus.Address = txtAddr.Text;
                        dbcus.City = txtCity.Text;
                        dbcus.Description = txtDesc.Text;
                        dbcus.Contacts = Convert.ToInt32(txtCnum.Text);
                        dbcus.Email_Address = txtEadd.Text;

                        dbcontext.Entry(dbcus).State = EntityState.Added;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        LVClientInfo.Enabled = true;
                    }
                    else
                    {
                        foreach (var c in add)
                        {
                            if (c.Customer_Name == txtCname.Text)
                            {
                                DialogResult YN = MessageBox.Show("You've entered an existing client, do you want to replace it?", "Data Exist", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                                if (YN == DialogResult.Yes)
                                {
                                    var replace = dbcontext.Customers.Find(c.Customerid_PK);
                                    replace.Customer_Name = txtCname.Text;
                                    replace.Address = txtAddr.Text;
                                    replace.City = txtCity.Text;
                                    replace.Contacts = Convert.ToInt32(txtCnum.Text);
                                    replace.Email_Address = txtEadd.Text;
                                    replace.Description = txtDesc.Text;

                                    dbcontext.Entry(replace).State = EntityState.Modified;
                                    dbcontext.SaveChanges();

                                    MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    RefreshFields();
                                    RefreshDB();
                                }
                            }
                            else
                            {
                                dbcus.Customer_Name = txtCname.Text;
                                dbcus.Address = txtAddr.Text;
                                dbcus.City = txtCity.Text;
                                dbcus.Description = txtDesc.Text;
                                dbcus.Contacts = Convert.ToInt32(txtCnum.Text);
                                dbcus.Email_Address = txtEadd.Text;

                                dbcontext.Entry(dbcus).State = EntityState.Added;
                                dbcontext.SaveChanges();

                                MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                RefreshFields();
                                RefreshDB();
                            }
                        }
                    }
                }

                else if(Datamanip == "Edit")
                {
                    DialogResult YN = MessageBox.Show("Do you want to replace it?", "Modify", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (YN == DialogResult.Yes)
                    {
                        var replace = dbcontext.Customers.Find(Convert.ToInt32(txtID.Text));
                        replace.Customer_Name = txtCname.Text;
                        replace.Address = txtAddr.Text;
                        replace.City = txtCity.Text;
                        replace.Contacts = Convert.ToInt32(txtCnum.Text);
                        replace.Email_Address = txtEadd.Text;
                        replace.Description = txtDesc.Text;

                        dbcontext.Entry(replace).State = EntityState.Modified;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();
                    }
                }
            }
        }

        private void LVClientInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            rowselect = LVClientInfo.FocusedItem.Index;

                txtID.Text = LVClientInfo.Items[rowselect].SubItems[0].Text;
                txtCname.Text = LVClientInfo.Items[rowselect].SubItems[1].Text;
                txtAddr.Text = LVClientInfo.Items[rowselect].SubItems[2].Text;
                txtCity.Text = LVClientInfo.Items[rowselect].SubItems[3].Text;
                txtDesc.Text = LVClientInfo.Items[rowselect].SubItems[4].Text;
                txtEadd.Text = LVClientInfo.Items[rowselect].SubItems[5].Text;
                txtCnum.Text = LVClientInfo.Items[rowselect].SubItems[6].Text;

                btnCDel.Enabled = true;
                btnCEdit.Enabled = true;
                btnCNew.Enabled = true;
                btnCSave.Enabled = false;
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            var cus = (from c in dbcontext.Customers
                       where c.Customer_Name.Contains(txtSearch.Text)
                       select c).ToList();

            LVClientInfo.Items.Clear();
            if (cus != null)
            {

                foreach (var sqlCus in cus)
                {
                    ListViewItem lvitems = LVClientInfo.Items.Add(sqlCus.Customerid_PK.ToString());
                    lvitems.SubItems.Add(sqlCus.Customer_Name.ToString());
                    lvitems.SubItems.Add(sqlCus.Address.ToString());
                    lvitems.SubItems.Add(sqlCus.City.ToString());
                    lvitems.SubItems.Add(sqlCus.Description.ToString());
                    lvitems.SubItems.Add(sqlCus.Email_Address.ToString());
                    lvitems.SubItems.Add(sqlCus.Contacts.ToString());

                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCDel_Click(object sender, EventArgs e)
        {
            DialogResult YN = MessageBox.Show("There are some records that will affect by this delete, Do you still want to proceed?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
             if (YN == DialogResult.Yes)
             {
                 var del = dbcontext.Customers.Find(Convert.ToInt32(txtID.Text));
                 if (del != null)
                 {

                     var proj = (from p in dbcontext.Projects
                                 where p.Customerid_FK == del.Customerid_PK
                                 select p).ToList();
                     if (proj.Count != null)
                     {
                         foreach (var itm in proj)
                         {
                             var labr = (from l in dbcontext.Labors
                                         where l.Projectid_FK == itm.Projectid_PK
                                         select l).ToList();
                             if (labr.Count != 0)
                             {
                                 foreach (var itms in labr)
                                 {
                                     //delete each laborer
                                     dbcontext.Entry(itms).State = EntityState.Deleted;
                                     dbcontext.SaveChanges();

                                 }
                                 MessageBox.Show("Laborer deleted");
                             }

                             var prjeq = (from pe in dbcontext.PrjEqs
                                          where pe.Projectid_FK == itm.Projectid_PK
                                          select pe).ToList();
                             if (prjeq.Count != 0)
                             {
                                 foreach (var items in prjeq)
                                 {
                                     var equip = dbcontext.Equipments.Find(items.Equipid_FK);
                                     equip.QtyUsed -= items.QuantityUsed;

                                     dbcontext.Entry(equip).State = EntityState.Modified;
                                     dbcontext.Entry(items).State = EntityState.Deleted;
                                     dbcontext.SaveChanges();
                                 }
                                 MessageBox.Show("Project-Equipment deleted");
                             }

                             var matest = (from m in dbcontext.Materials
                                           where m.Projectid_FK == itm.Projectid_PK
                                           select m).ToList();
                             if (matest.Count != 0)
                             {
                                 foreach (var item in matest)
                                 {
                                     dbcontext.Entry(item).State = EntityState.Deleted;
                                     dbcontext.SaveChanges();
                                 }
                                 MessageBox.Show("Materials Estimation deleted");
                             }

                             dbcontext.Entry(itm).State = EntityState.Deleted;
                             dbcontext.SaveChanges();
                         }

                         
                     }

                     dbcontext.Entry(del).State = EntityState.Deleted;
                     dbcontext.SaveChanges();
                 }
                 RefreshFields();
                 RefreshDB();
             }
        }

        private void btnCPrint_Click(object sender, EventArgs e)
        {
            ClientRptForm crpt = new ClientRptForm();

            crpt.Show();
        }

    }
}
